/*
The House object will be the main controller for a level. It will handel loading the resources, controlling the play, NPC.. ect.
Although it's currently the main game controller, I plan on adding one a level above. That one will pretty much be the main menu and will handel going from level to level.

*/
#include "house.h"
#include "player.h"
#include "sprite.h"
#include "stdlib.h"
#include "SDL.h"
#include "SDL_Draw.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <stdio.h>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include "SDL_draw.h"
#include <cmath>
#include <zipArchive.h>
#include <stdafx.h>
#include "gui.h"
#include "SDL_mixer.h"
#include <iostream>
#include <fstream>
#include <iomanip>
const int PROC_RATE=33;

house::house(){

	Mix_Music *music;
	font1 = NULL;
	screen = NULL;
	CZipArchive zip; 

	zip.Open(_T("data.zip"));
	try{
		for (int i = 0; i < zip.GetCount(); i++ ){
			zip.ExtractFile(i,_T("temp"),false);
		}
	}catch(...){
		zip.Close(CZipArchive::afAfterException);
	}

	top = new SDL_Surface();
	background = new SDL_Surface();
	test = new SDL_Surface();
	run = true;
	srand(time(0));
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){
		return; 
	} 
	
	screen = SDL_SetVideoMode( 1024, 768,32, SDL_SWSURFACE );
	
	if (screen==NULL){
		return;
	}
	
	Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 4096);
	TTF_Init();

	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

	SDL_WM_SetCaption("PBC POC",NULL);


	music=Mix_LoadMUS("temp/song.mp3");
	if(music) {
		Mix_PlayMusic(music, 0);
	}

	Ticks=SDL_GetTicks();
	FPS=0;
	Frames=0;
	procTicks=0;
	procState=0;
	font1 = TTF_OpenFont("temp/font1.ttf",20);
	background = loadImage( "temp/background.png" ); 
	test = loadImage( "temp/ball.tga" ); 
	s_player = new sprite("temp/ship.png","temp/ship.sis",this);

	Player = new player(this);
/*	housegui = new Gui(screen);
	pane* temp2 = new pane();
	housegui->AddChild(temp2);
	pane* temp = new pane();
	SDL_Rect t={100,100,50,20};
	temp->SetRect(t);
	housegui->AddChild(temp);
	pane* temp3 = new pane();
	temp->AddChild(temp3);*/

	this->loadEIF();

	this->tick();
}

void house::loadEIF(){
	char tempcstr[256];
	
	std::ifstream EIF("temp/level.EIF");
	EIF.getline(tempcstr,256);
	std::string tempstr(tempcstr);
	
	size_t found;
	found = tempstr.find_first_of("|");
	std::string start;
	start = tempstr.substr(0,found);
	tempstr = tempstr.substr(found);
SDL_Delay(10);
}


void house::tick(){
	procFrames=SDL_GetTicks();
	while(run){
		while( SDL_PollEvent( &event ) ){
			if( event.type == SDL_QUIT ){
				run=false;
			}
			//static_cast<Gui*>(housegui)->Proccess(&event);
		}
		
		Uint8 *keystates = SDL_GetKeyState( NULL ); 



		procTicks+=(SDL_GetTicks()-procFrames);

		
		while (procTicks>PROC_RATE) {
			procState+=1;
			procTicks-=PROC_RATE;
		}

		procFrames=SDL_GetTicks();



		SDL_Color textColor = { 255, 255, 255 }; 
		SDL_Surface *message = NULL; 
		applySurface(0,0,background,screen);


		Player->Draw();
		//housegui->Draw();
		applySurface(0,0,top,screen);


		std::stringstream b;
		if ((SDL_GetTicks()-Ticks)>1000){
			FPS=Frames;
			Ticks=SDL_GetTicks();
			Frames=0;
		}
		
		b << "FPS: " << FPS << " State: "<< procState << " " << procFrames << " ";
		if (procState!=0)
			b << (procFrames/procState);
		message = TTF_RenderText_Solid( font1, b.str().c_str() , textColor ); 
		applySurface(0,0,message,screen);

		if( SDL_Flip(screen)==-1){
			return;
		}
		Frames+=1;
	}
}
		
		

SDL_Surface* house::loadImage( std::string filename ) { 
	SDL_Surface* loadedImage = NULL; 
	SDL_Surface* optimizedImage = NULL; 
	loadedImage = IMG_Load( filename.c_str() ); 
	if( loadedImage != NULL ) { 
		optimizedImage = SDL_DisplayFormat( loadedImage ); 	
		if (optimizedImage!=NULL){
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0, 0xFF );
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
		}
		SDL_FreeSurface( loadedImage ); 
	} 
	return optimizedImage; 
}

void house::applySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { 
	SDL_Rect offset; 
	offset.x = x; 
	offset.y = y; 
	SDL_BlitSurface( source, NULL, destination, &offset ); 
}